package models.my;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import models.user.UserInfo;

import play.Logger;
import play.db.ebean.Model;
import utils.MengException;

/**
 * 用户积分
 * @author zhangpeng
 *
 */

@Entity
@Table(name="user_score")
public class MyScore extends Model{

	private static final long serialVersionUID = 1L;
	
	@Id
	public long id;
	public String username;
	public long score = 0;
	
	public static MyScore getInstances(){
		return new MyScore();
	}
	
	public static Model.Finder<Long, MyScore> find = new Model.Finder<Long, MyScore>(Long.class, MyScore.class);
	
	/**
	 * 创建用户积分
	 * 
	 * @param score
	 * @return
	 * @throws MengException
	 */
	public boolean createScore(MyScore score) throws MengException {

		if (UserInfo.getInstances().verifyUsername(score.username)) {
			throw new MengException("900001", "用户名不存在");
		}

		if (verifyMyScore(score.username)) {
			throw new MengException("900002", "用户积分已存在");
		}

		try {
			score.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createScore Exception," + t.getMessage());
			throw new MengException("900003", "用户积分保存失败");
		}
	}

	public boolean createScore(UserInfo ui) throws MengException {

		if (verifyMyScore(ui.username)) {
			throw new MengException("900008", "用户基本信息已存在");
		}
		MyScore score = getInstances();
		score.username = ui.username;
		try {
			score.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createBUI Exception," + t.getMessage());
			throw new MengException("900009", "用户基本信息保存失败");
		}
	}

	/**
	 * 用户积分修改
	 * 
	 * @param score
	 * @return
	 * @throws MengException
	 */
	public boolean modifyScore(MyScore score) throws MengException {

		if (UserInfo.getInstances().verifyUsername(score.username)) {
			throw new MengException("900004", "用户名不存在");
		}
		score.id = getScore(score.username).id;

		try {
			score.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyScore Exception," + t.getMessage());
			throw new MengException("900005", "用户积分修改失败");
		}
	}
	
	public boolean addScore(MyScore score) throws MengException {

		if (UserInfo.getInstances().verifyUsername(score.username)) {
			throw new MengException("900010", "用户名不存在");
		}
		MyScore ms = getScore(score.username);
		score.id = ms.id;
		score.score += ms.score;
		try {
			score.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyScore Exception," + t.getMessage());
			throw new MengException("900011", "用户积分修改失败");
		}
	}
	
	public boolean subScore(MyScore score) throws MengException {

		if (UserInfo.getInstances().verifyUsername(score.username)) {
			throw new MengException("900012", "用户名不存在");
		}
		
		MyScore ms = getScore(score.username);
		score.id = ms.id;
		score.score = ms.score - score.score;

		try {
			score.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyScore Exception," + t.getMessage());
			throw new MengException("900013", "用户积分修改失败");
		}
	}

	/**
	 * 获取用户积分
	 * 
	 * @param username
	 * @return
	 * @throws MengException
	 */
	
	public MyScore getScore(String username) throws MengException {

		if (UserInfo.getInstances().verifyUsername(username)) {
			throw new MengException("900006", "用户名不存在");
		}

		try {

			return find.where().eq("username", username).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getScore Exception," + t.getMessage());
			throw new MengException("900007", "获取用户积分失败");
		}
	}
	
	public boolean verifyMyScore(String username) throws MengException {

		if (getScore(username) != null) {
			return true;
		}

		return false;
	}

}
